package org.jinyan.quartz.example4;

import java.util.Date;

import org.quartz.DateBuilder;
import org.quartz.JobBuilder;
import org.quartz.JobDetail;
import org.quartz.ScheduleBuilder;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.SimpleScheduleBuilder;
import org.quartz.Trigger;
import org.quartz.TriggerBuilder;
import org.quartz.impl.StdSchedulerFactory;

public class JobStateExample {

	public static int count = 0;

	public void startQuartz() throws SchedulerException {

		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sch = sf.getScheduler();

		JobDetail job1 = JobBuilder.newJob(ColorJob.class)
				.withIdentity("job1", "group1").build();
		Date startTime = DateBuilder.nextGivenSecondDate(null, 10);
		Trigger trigger = TriggerBuilder
				.newTrigger()
				.withIdentity("trigger", "group1")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.withIntervalInSeconds(10).withRepeatCount(10))
				.startAt(startTime).build();
		job1.getJobDataMap().put(ColorJob.FAVORITECOLORKEY, "red");
		job1.getJobDataMap().put(ColorJob.CountKey, count++);
		Date nextDate = sch.scheduleJob(job1, trigger);

		JobDetail job2 = JobBuilder.newJob(ColorJob.class)
				.withIdentity("job2", "group2").build();
		Date triggerStartTime = DateBuilder.nextGivenSecondDate(null, 20);
		Trigger trigger2 = TriggerBuilder
				.newTrigger()
				.withIdentity("trigger1", "group1")
				.withSchedule(
						SimpleScheduleBuilder.simpleSchedule()
								.withRepeatCount(5).withIntervalInSeconds(5))
				.startAt(triggerStartTime).build();
		job2.getJobDataMap().put(ColorJob.CountKey, count--);
		job2.getJobDataMap().put(ColorJob.FAVORITECOLORKEY, "green");
		Date dt = sch.scheduleJob(job2, trigger2);
		sch.start();

	}

	public static void main(String[] args) throws SchedulerException {
		new JobStateExample().startQuartz();
	}

}
